package com.benio.ycfdisplayer.imageloader;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.benio.ycfdisplayer.displayer.ImageViewTarget;
import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.Target;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.imageaware.ViewAware;

/**
 * 将Options应用到{@link com.nostra13.universalimageloader.core.ImageLoader}
 * Created by benio on 2016/11/3.
 */
class UILOptionsApplier {

    /**
     * 将{@link Options}包装成{@link DisplayImageOptions}
     */
    public static DisplayImageOptions applyOptions(Options o) {
        if (o == null) {
            return null;
        }
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        if (o.failureImage != null) {
            builder.showImageOnFail(o.failureImage);
        }
        if (o.placeHolderImage != null) {
            builder.showImageOnLoading(o.placeHolderImage);
        }
        if (!(o instanceof UILDisplayOptions)) {
            return builder.build();
        }
        UILDisplayOptions options = (UILDisplayOptions) o;
        if (options.emptyUriImage != null) {
            builder.showImageForEmptyUri(options.emptyUriImage);
        }
        if (options.decodingOptions != null) {
            builder.decodingOptions(options.decodingOptions);
        }
        if (options.extraForDownloader != null) {
            builder.extraForDownloader(options.extraForDownloader);
        }
        if (options.preProcessor != null) {
            builder.preProcessor(options.preProcessor);
        }
        if (options.postProcessor != null) {
            builder.postProcessor(options.postProcessor);
        }
        if (options.displayer != null) {
            builder.displayer(options.displayer);
        }
        if (options.handler != null) {
            builder.handler(options.handler);
        }

        return builder.resetViewBeforeLoading(options.resetViewBeforeLoading)
                .cacheInMemory(options.cacheInMemory)
                .cacheOnDisk(options.cacheOnDisk)
                .imageScaleType(options.imageScaleType)
                .delayBeforeLoading(options.delayBeforeLoading)
                .considerExifParams(options.considerExifParams)
                .build();
    }

    /**
     * 将{@link Target}包装成{@link ImageAware}
     */
    public static ImageAware targetToImageAware(final Target target) {
        if (target == null) {
            return null;
        }
        ImageAware imageAware = null;
        if (target instanceof ImageViewTarget) {
            imageAware = new ImageViewAware((ImageView) target.getView()) {
                @Override
                public boolean setImageDrawable(Drawable drawable) {
                    return target.setDrawable(drawable);
                }

                @Override
                public boolean setImageBitmap(Bitmap bitmap) {
                    return target.setBitmap(bitmap);
                }
            };
        } else {
            imageAware = new ViewAware(target.getView()) {
                @Override
                public boolean setImageBitmap(Bitmap bitmap) {
                    return target.setBitmap(bitmap);
                }

                @Override
                public boolean setImageDrawable(Drawable drawable) {
                    return target.setDrawable(drawable);
                }

                @Override
                protected void setImageDrawableInto(Drawable drawable, View view) {
                }

                @Override
                protected void setImageBitmapInto(Bitmap bitmap, View view) {
                }
            };
        }
        return imageAware;
    }
}
